<template>
  <canvas ref="container" class="w-full h-full"></canvas>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { WebGL } from "../../lib/webgl/WebGL";
import { getMvpMatrix } from "../../lib/math/GetMatrix";

const container = ref(null);
const webgl = new WebGL();
let oldTime = Date.now();

const cube = {
  vertex: new Float32Array([
    // float3 position, float2 uv
    // face1
    +1, -1, +1, 1, 1, -1, -1, +1, 0, 1, -1, -1, -1, 0, 0, +1, -1, -1, 1, 0, +1,
    -1, +1, 1, 1, -1, -1, -1, 0, 0,
    // face2
    +1, +1, +1, 1, 1, +1, -1, +1, 0, 1, +1, -1, -1, 0, 0, +1, +1, -1, 1, 0, +1,
    +1, +1, 1, 1, +1, -1, -1, 0, 0,
    // face3
    -1, +1, +1, 1, 1, +1, +1, +1, 0, 1, +1, +1, -1, 0, 0, -1, +1, -1, 1, 0, -1,
    +1, +1, 1, 1, +1, +1, -1, 0, 0,
    // face4
    -1, -1, +1, 1, 1, -1, +1, +1, 0, 1, -1, +1, -1, 0, 0, -1, -1, -1, 1, 0, -1,
    -1, +1, 1, 1, -1, +1, -1, 0, 0,
    // face5
    +1, +1, +1, 1, 1, -1, +1, +1, 0, 1, -1, -1, +1, 0, 0, -1, -1, +1, 0, 0, +1,
    -1, +1, 1, 0, +1, +1, +1, 1, 1,
    // face6
    +1, -1, -1, 1, 1, -1, -1, -1, 0, 1, -1, +1, -1, 0, 0, +1, +1, -1, 1, 0, +1,
    -1, -1, 1, 1, -1, +1, -1, 0, 0,
  ]),
  vertexCount: 36,
};

function initShader(gl) {
  let vertexShader = gl.createShader(gl.VERTEX_SHADER);
  gl.shaderSource(
    vertexShader,
    `
    attribute vec3 a_Position;
    varying vec4 v_Position;
    uniform mat4 u_MvpMatrix;
    void main() {
      gl_Position = u_MvpMatrix * vec4(a_Position, 1.0);
      v_Position = (vec4(a_Position, 1.0) + vec4(1.0, 1.0, 1.0, 1.0)) * 0.5;
    }
    `
  );
  gl.compileShader(vertexShader);

  let fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
  gl.shaderSource(
    fragmentShader,
    `
    precision mediump float;
    varying vec4 v_Position;
    void main() {
      gl_FragColor = v_Position;
    }
    `
  );
  gl.compileShader(fragmentShader);

  let program = gl.createProgram();
  gl.attachShader(program, vertexShader);
  gl.attachShader(program, fragmentShader);
  gl.linkProgram(program);

  gl.useProgram(program);
  gl.program = program;

  gl.enable(gl.DEPTH_TEST);
}

function draw(gl) {
  let vertexBuffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  gl.bufferData(gl.ARRAY_BUFFER, cube.vertex, gl.STATIC_DRAW);

  let a_Position = gl.getAttribLocation(gl.program, "a_Position");
  gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, 5 * 4, 0);
  gl.enableVertexAttribArray(a_Position);
}

function tick(gl, mvpMatrix) {
  let u_MvpMatrix = gl.getUniformLocation(gl.program, "u_MvpMatrix");
  gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix);

  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  gl.clear(gl.COLOR_BUFFER_BIT);

  gl.drawArrays(gl.TRIANGLES, 0, cube.vertexCount);
}

function init() {
  let canvas = container.value;
  let gl = webgl.initWebGL(canvas);
  initShader(gl);

  let aspect = canvas.width / canvas.height;
  let position = { x: 0, y: 0, z: -5 };
  let rotation = { x: 0.5, y: 0.5, z: 0 };
  let scale = { x: 1, y: 1, z: 1 };
  draw(gl);

  function frame() {
    let newTime = Date.now();
    let delta = (newTime - oldTime) / 1000;
    oldTime = newTime;

    rotation.x += delta * 1;
    rotation.y += delta * 1;
    rotation.z += delta * 1;
    let mvpMatrix = getMvpMatrix(aspect, position, rotation, scale);
    tick(gl, mvpMatrix);

    requestAnimationFrame(frame);
  }
  frame();

  window.addEventListener("resize", () => {
    canvas.width = canvas.clientWidth;
    canvas.height = canvas.clientHeight;
    aspect = canvas.width / canvas.height;
    gl.viewport(0, 0, canvas.width, canvas.height);
  });
}

onMounted(() => {
  init();
});
</script>
